package com.cs.dao;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.cs.entity.Course;
import com.cs.entity.Grade;
import com.cs.entity.Student;

@Repository
@Transactional
public class StudentDaoImpl {
	@Resource
	private SessionFactory sessionFactory;
	
	//学生登录
	public Student studentLodin(String sno,String spassword){
		Query query = this.sessionFactory.getCurrentSession().createQuery("from Student where sno=? and spassword=?");
		query.setParameter(0, sno);
		query.setParameter(1, spassword);
		Student student = (Student) query.uniqueResult();
		return student;
	}
	//根据id查询个人信息
	public Student findStudentById(Integer id) {
		Query query = this.sessionFactory.getCurrentSession().createQuery("from Student where id=?");
		query.setParameter(0, id);
		Student student = (Student) query.uniqueResult();
		return student;
	}
	//修改个人信息
	public boolean updateStudent(Student student){
		this.sessionFactory.getCurrentSession().update(student);
		return true;
	}
	//查看可选课程(对应专业以及status为1(选修课)的)
	public List<Course> findOptionalCourses(String specialities) {
		Query query = this.sessionFactory.getCurrentSession().createQuery("from Course where cspecialities=? and status=1");
		query.setParameter(0, specialities);
		List<Course> list = query.list();
		return list;
	}
	//根据id查询课程
	public Course findCourseById(Integer id) {
		Query query = this.sessionFactory.getCurrentSession().createQuery("from Course where id=?");
		query.setParameter(0, id);
		Course course = (Course) query.uniqueResult();
		return course;
	}
	//选课
	public void electiveCourse(Student student,Course course) {
		this.sessionFactory.getCurrentSession().update(course);
		this.sessionFactory.getCurrentSession().update(student);
	}
	//查看成绩
	public List<Grade> selectGrade(Integer id){
		Query query = this.sessionFactory.getCurrentSession().createQuery("from Grade g where g.student.id=?");
		query.setParameter(0, id);
		List<Grade> list = query.list();
		return list;
	}
	//查询已选课程
	

}
